package com.token;

import io.jsonwebtoken.Claims;
import io.jsonwebtoken.JwtBuilder;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;
import org.springframework.stereotype.Component;

import javax.crypto.spec.SecretKeySpec;
import javax.xml.bind.DatatypeConverter;
import java.security.Key;
import java.util.Date;

@Component
public class JwtUtil {

    //密码 。绝对保密，写在配置文件中
    public static String serceKey="mingtianhenganghao";
    public final static long keeptime=1800000;

   /* @Value("${token.sercetKey}")
    public  static String sercetKey;
    @Value("${token.keeptime:30000}")
    public static long keeptime;*/


   public static void main(String[] args){
       String token=JwtUtil.generToken("admin",null,null);
       System.out.println(token);
       token="eyJhbGciOiJIUzI1NiJ9.eyJqdGkiOiJhZG1pbiIsImlhdCI6MTU3MzQzMjk4MCwiZXhwIjoxNTczNDM0NzgwfQ.B3935lyhGjOhDmK0ZfoamZQxWj1ib5EnEWybF3npefA";
String username=JwtUtil.getUsername(token);
       System.out.println(username);


   }


    /**
     * 产生令牌
     * @param id 用户名
     * @param issuer 签发著
     * @param subject subject 主体（内容）
     * @return
     */
   public static String generToken(String id,String issuer,String subject){
       long ttlMillis=keeptime;
       SignatureAlgorithm signatureAlgorithm=SignatureAlgorithm.HS256;
       long nowMillis=System.currentTimeMillis();
       Date now=new Date(nowMillis);
       byte[] apiKeySecretBytes= DatatypeConverter.parseBase64Binary(serceKey);
       Key signingKey=new SecretKeySpec(apiKeySecretBytes,signatureAlgorithm.getJcaName());
       JwtBuilder builder= Jwts.builder().setId(id).setIssuedAt(now);
       if (subject!=null){
           builder.setSubject(subject);
       }
       if (issuer!=null){
           builder.setIssuer(issuer);
       }
       builder.signWith(signatureAlgorithm,signingKey);
       if (ttlMillis>=0){
           long expMills=nowMillis+ttlMillis;
           Date exp=new Date(expMills);
           builder.setExpiration(exp);
       }
       return builder.compact();
   }

    /**
     * 更新令牌
     * @param token
     * @return
     */
   public static String updateToken(String token){
       try{
           Claims claims=verifyToken(token);
           String id=claims.getId();
           String subject=claims.getSubject();
           String issuer=claims.getIssuer();
           Date date=claims.getExpiration();
           return generToken(id,issuer,subject);
       }catch (Exception e){
//           e.printStackTrace();
       }
       return "";
   }

    /**
     * 获取用户名
     * @param token
     * @return
     */
   public static String getUsername(String token){
       try{
           Claims claims=Jwts.parser().setSigningKey(DatatypeConverter.parseBase64Binary(serceKey)).parseClaimsJws(token).getBody();
           return claims.getId();
       }catch (Exception e){
//           e.printStackTrace();
       }
       return null;
   }

   public static Claims verifyToken(String token){
       Claims claims=Jwts.parser().setSigningKey(DatatypeConverter.parseBase64Binary(serceKey)).parseClaimsJws(token).getBody();
       return claims;
   }


}
